package com.idopartx.phonelightning.viewmodel

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.idopartx.phonelightning.common.Constant
import com.idopartx.phonelightning.entity.CallLightningCache
import com.idopartx.phonelightning.entity.CallLightningEntity
import com.idopartx.phonelightning.entity.LightNightVo
import com.idopartx.phonelightning.utils.MorseUtils
import com.tencent.mmkv.MMKV

class CallLightingViewModel : ViewModel() {

    val titleLiveData: MutableLiveData<String> by lazy {
        MutableLiveData()
    }

    val presetListLiveData: MutableLiveData<ArrayList<CallLightningEntity>> by lazy {
        MutableLiveData()
    }

    val commonListLiveData: MutableLiveData<MutableList<CallLightningEntity>> by lazy {
        MutableLiveData()
    }

    val lightNightLiveData: MutableLiveData<MutableList<LightNightVo>> by lazy {
        MutableLiveData()
    }

    val editStatusLiveData: MutableLiveData<Boolean> by lazy {
        MutableLiveData()
    }

    fun getCommonList() {
        val callLightningCache = MMKV.defaultMMKV()
            .decodeParcelable(Constant.MMKV_SAVE_CALL_LIGHTNING, CallLightningCache::class.java)
        if (callLightningCache == null) {
            MMKV.defaultMMKV()
                .encode(Constant.MMKV_SAVE_CALL_LIGHTNING, CallLightningCache(arrayListOf()))
        }
        commonListLiveData.value = callLightningCache?.callLightningList
    }

    fun getPresetList() {

        val presetCache = MMKV.defaultMMKV().decodeParcelable(
            Constant.MMKV_SAVE_PRESET_CALL_LIGHTNING,
            CallLightningCache::class.java
        )
        if (presetCache == null) {
            val presetList = arrayListOf<CallLightningEntity>()

            val loveEntity = CallLightningEntity(
                "love",
                true,
                MorseUtils.trans2Lightning(MorseUtils.trans2Morse("LOVE"))
            )
            presetList.add(loveEntity)

            val sosEntity = CallLightningEntity(
                "sos",
                true,
                MorseUtils.trans2Lightning(MorseUtils.trans2Morse("SOS"))
            )
            presetList.add(sosEntity)

            val heartList = arrayListOf<LightNightVo>()
            heartList.add(LightNightVo(200, 200))
            heartList.add(LightNightVo(200, 500))
            val heartEntity = CallLightningEntity("心跳", false, heartList)
            presetList.add(heartEntity)

            val lightningList = arrayListOf<LightNightVo>()
            lightningList.add(LightNightVo(10, 10))
            val lightningEntity = CallLightningEntity("爆闪", false, lightningList)
            presetList.add(lightningEntity)

            val knockDoorList = arrayListOf<LightNightVo>()
            knockDoorList.add(LightNightVo(150, 150))
            knockDoorList.add(LightNightVo(150, 150))
            knockDoorList.add(LightNightVo(150, 500))
            val knockDoorEntity = CallLightningEntity("敲门", false, knockDoorList)
            presetList.add(knockDoorEntity)
            MMKV.defaultMMKV().encode(Constant.MMKV_SAVE_PRESET_CALL_LIGHTNING, CallLightningCache(presetList))
            presetListLiveData.value = presetList
        } else {
            presetListLiveData.value = presetCache.callLightningList
        }

    }
}